﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mimoza.ObjectLayer;
using Mimoza.Apps.Console.Commands;

namespace Mimoza.Apps.Console.ViewModel
{
    public abstract class HostsViewModelBase : BaseViewModel
    {
        public virtual IEnumerable<Mimoza.ObjectLayer.Host> GetHosts() { return null; }
        public virtual Container CurrentContainer { get { return null; } set { } }
    }

    public class HostsViewModel : HostsViewModelBase
    {
        private Container m_currentContainer = null;

        public override Container CurrentContainer
        {
            get { return m_currentContainer; }
            set
            {
                if (m_currentContainer != value)
                {
                    m_currentContainer = value;
                    NotifyPropertyChanged("CurrentContainer");

                    this.Context = null;
                }
            }
        }
        public override void Init(Mimoza.Apps.Console.Application application)
        {
            base.Init(application);

            m_application.ContainersTreeViewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(ContainersTreeViewModel_PropertyChanged);
            m_application.ObjectLayer.HostRepository.OnObjectChanged += new EventHandler<ObjectLayer_ObjectChangedEventArgs>(HostRepository_OnObjectChanged);

            m_ToolBarCommands.Add(new MoveHostCommand(this));
            m_ToolBarCommands.Add(new RenameHostCommand(this));
            m_ToolBarCommands.Add(new NetworkSettingsCommand(this));
            m_ToolBarCommands.Add(new DeleteHostCommand(this));

            m_ContextCommands.Add(new MoveHostCommand(this));
            m_ContextCommands.Add(new RenameHostCommand(this));
            m_ContextCommands.Add(new DeleteHostCommand(this));
            m_ContextCommands.Add(null);
            m_ContextCommands.Add(new DesktopConnectCommand(this));
            m_ContextCommands.Add(null);
            m_ContextCommands.Add(new NetworkSettingsCommand(this));
        }

        void HostRepository_OnObjectChanged(object sender, ObjectLayer_ObjectChangedEventArgs e)
        {
            RaiseOnObjectChanged(e);
        }

        void ContainersTreeViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Context")
            {
                Container cont = (sender as BaseViewModel).Context as Container;

                if (null != cont && (cont.Type == ContainerType.Managed || cont.Type == ContainerType.Unmanaged))
                {
                    this.CurrentContainer = cont;
                    this.Visible = true;
                }
                else
                {
                    this.CurrentContainer = null;
                    this.Visible = false;
                }
            }
        }
        
        public override IEnumerable<Mimoza.ObjectLayer.Host> GetHosts()
        {
            if (m_currentContainer != null)
            {
                foreach (Mimoza.ObjectLayer.Host host in m_currentContainer.GetHosts())
                {
                    yield return host;
                }
            }
            else
            {
                yield break;
            }
        }
    }
}
